using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Musicman.Client
{
    public partial class LoginForm : Form
    {
        private AuthenticationService.AuthenticationService aService;
        private AuthenticationService.AuthHeader aHeader;
        private string userPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal).ToString() + "\\user.srl";
        private Musicman.Client.AuthenticationService.User user;
        private bool rememberMe = false;
        private int userLogins;
        private int totalLogins;

        public LoginForm(AuthenticationService.AuthenticationService aService, AuthenticationService.AuthHeader aHeader)
        {
            InitializeComponent();
            this.aService = aService;
            this.aHeader = aHeader;
            if (File.Exists(userPath))
            {
                Musicman.Client.AuthenticationService.User u = DeserializeUser();
                this.user = u;
                this.txtEmail.Text = u.Emailaddress;
                this.txtPassword.Text = u.Password;
                this.cbRemeberMe.Checked = true;
                this.rememberMe = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Musicman.Client.AuthenticationService.User user = new Musicman.Client.AuthenticationService.User();
            user.Emailaddress = txtEmail.Text;
            user.Password = txtPassword.Text;
            aHeader.user = user;
            aService.AuthHeaderValue = aHeader;

            bool success = aService.LoggIn();
            if (success)
            {
                this.user = aService.AuthHeaderValue.user;
                this.userLogins = aService.AuthHeaderValue.userLogs;
                this.totalLogins = aService.AuthHeaderValue.totalLogs;
                if (!this.rememberMe && File.Exists(userPath))
                {
                    File.Delete(userPath);
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.label2.Visible = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        public Musicman.Client.AuthenticationService.User DeserializeUser()
        {
            FileStream fs = File.Open(userPath, FileMode.Open, FileAccess.Read);
            BinaryFormatter bf = new BinaryFormatter();
            Musicman.Client.AuthenticationService.User u = (Musicman.Client.AuthenticationService.User)bf.Deserialize(fs);
            fs.Close();
            return u;
        }

        public Musicman.Client.AuthenticationService.User User
        {
            get { return user; }
            set { user = value; }
        }
        
        public bool RememberMe
        {
            get { return rememberMe; }
            set { rememberMe = value; }
        }

        public int UserLogins
        {
            get { return userLogins; }
            set { userLogins = value; }
        }

        public int TotalLogins
        {
            get { return totalLogins; }
            set { totalLogins = value; }
        }

        private void cbRemeberMe_CheckedChanged(object sender, EventArgs e)
        {
            this.rememberMe = ((CheckBox)sender).Checked;
        }
        
        private void LoginForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics gr = e.Graphics;
            SolidBrush sb = new SolidBrush(Color.Black);
            Pen p = new Pen(sb);
            p.Width = 2;
            gr.DrawRectangle(p, 1, 1, this.Width - 2 , this.Height - 2);
        }
    }
}